﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using ihongma.DBUtility;//Please add references
namespace ihongma.DAL
{
	/// <summary>
	/// 数据访问类:color
	/// </summary>
	public partial class color
	{
		public color()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperOleDb.GetMaxID("id", "dt_color"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from dt_color");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
};
			parameters[0].Value = id;

			return DbHelperOleDb.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(ihongma.Model.color model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into dt_color(");
			strSql.Append("name,colorvalue,hits,addtime)");
			strSql.Append(" values (");
			strSql.Append("@name,@colorvalue,@hits,@addtime)");
			OleDbParameter[] parameters = {
					new OleDbParameter("@name", OleDbType.VarChar,255),
					new OleDbParameter("@colorvalue", OleDbType.VarChar,255),
					new OleDbParameter("@hits", OleDbType.Integer,4),
					new OleDbParameter("@addtime", OleDbType.Date)};
			parameters[0].Value = model.name;
			parameters[1].Value = model.colorvalue;
			parameters[2].Value = model.hits;
			parameters[3].Value = model.addtime;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ihongma.Model.color model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update dt_color set ");
			strSql.Append("name=@name,");
			strSql.Append("colorvalue=@colorvalue,");
			strSql.Append("hits=@hits,");
			strSql.Append("addtime=@addtime");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@name", OleDbType.VarChar,255),
					new OleDbParameter("@colorvalue", OleDbType.VarChar,255),
					new OleDbParameter("@hits", OleDbType.Integer,4),
					new OleDbParameter("@addtime", OleDbType.Date),
					new OleDbParameter("@id", OleDbType.Integer,4)};
			parameters[0].Value = model.name;
			parameters[1].Value = model.colorvalue;
			parameters[2].Value = model.hits;
			parameters[3].Value = model.addtime;
			parameters[4].Value = model.id;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from dt_color ");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
};
			parameters[0].Value = id;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from dt_color ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ihongma.Model.color GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,name,colorvalue,hits,addtime from dt_color ");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
};
			parameters[0].Value = id;

			ihongma.Model.color model=new ihongma.Model.color();
			DataSet ds=DbHelperOleDb.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["name"]!=null && ds.Tables[0].Rows[0]["name"].ToString()!="")
				{
					model.name=ds.Tables[0].Rows[0]["name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["colorvalue"]!=null && ds.Tables[0].Rows[0]["colorvalue"].ToString()!="")
				{
					model.colorvalue=ds.Tables[0].Rows[0]["colorvalue"].ToString();
				}
				if(ds.Tables[0].Rows[0]["hits"]!=null && ds.Tables[0].Rows[0]["hits"].ToString()!="")
				{
					model.hits=int.Parse(ds.Tables[0].Rows[0]["hits"].ToString());
				}
				if(ds.Tables[0].Rows[0]["addtime"]!=null && ds.Tables[0].Rows[0]["addtime"].ToString()!="")
				{
					model.addtime=DateTime.Parse(ds.Tables[0].Rows[0]["addtime"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,name,colorvalue,hits,addtime ");
			strSql.Append(" FROM dt_color ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			OleDbParameter[] parameters = {
					new OleDbParameter("@tblName", OleDbType.VarChar, 255),
					new OleDbParameter("@fldName", OleDbType.VarChar, 255),
					new OleDbParameter("@PageSize", OleDbType.Integer),
					new OleDbParameter("@PageIndex", OleDbType.Integer),
					new OleDbParameter("@IsReCount", OleDbType.Boolean),
					new OleDbParameter("@OrderType", OleDbType.Boolean),
					new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
					};
			parameters[0].Value = "dt_color";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

